package com.xudow.app.newui;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.activeshare.edu.ucenter.models.user.AgencyTeacher;
import com.activeshare.edu.ucenter.models.user.InspectionTeam;
import com.makeramen.roundedimageview.RoundedImageView;
import com.xudow.app.Config;
import com.xudow.app.R;
import com.xudow.app.dynamicstate_old.module.PictureActivity;
import com.xudow.app.newui.base.BaseUiActivity;
import com.xudow.app.newui.task.InspectionTeamTask;
import com.xudow.app.ui.task.AgencyTeacherTask;
import com.xudow.app.util.ImageUtils;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;

import butterknife.BindView;
import it.sephiroth.android.library.widget.AdapterView;
import it.sephiroth.android.library.widget.HListView;

/**
 * 机构老师详情
 */
public class AgencyTeacherActivity extends BaseUiActivity implements AdapterView.OnItemClickListener {

    @BindView(R.id.img_list)
    HListView team_list;

    @BindView(R.id.career_beging_tv)
    TextView career_beging_tv;//从业时间

    @BindView(R.id.teacher_introduce_tv)
    TextView teacher_introduce_tv;//教师简介

    @BindView(R.id.performance_tv)
    TextView performance_tv;//成果

    @BindView(R.id.experince_tv)
    TextView experince_tv;//经历

    @BindView(R.id.teacher_name_tv)
    TextView teacher_name_tv;//教师名称

    private String agencyTeacherId="";
    ImageAdapter myadapter;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_agency_teacher);
        myadapter=new ImageAdapter(this);
        team_list.setAdapter(myadapter);

    }

    @Override
    protected void initData() {
        agencyTeacherId=getIntent().getStringExtra("agencyTeacherId");
        HashMap<String,String> map=new HashMap<String,String>();
        map.put("agencyTeacherId",agencyTeacherId);
        AgencyTeacherTask task=new AgencyTeacherTask(this,getTeamDataHandler);
        task.execute(map);
    }
    private AgencyTeacher teacher;
    List<String> imgs;
    private Handler getTeamDataHandler=new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            if (msg.what==InspectionTeamTask.SUCCESS){
                teacher=(AgencyTeacher)msg.obj;

                if (null!=teacher){
                    UpdateTeamInfo();
                    String img=teacher.getImagePath();
                    if (null!=img&&!img.equals("")){
                        imgs=new ArrayList<String>();
                        if (img.contains(";")){

                            for (String str:img.split(";")) {
                                imgs.add(str);
                            }
                        }else
                            imgs.add(img);
                        team_list.setVisibility(View.VISIBLE);
                        myadapter.setData(imgs);

                    }else {
                        team_list.setVisibility(View.GONE);
                    }
                }
            }else
                getMyApp().makeToast(msg.getData().getString("errMessage"));
        }
    };
    public void onBackBtnClick(View view) {
        finish();
    }

    @Override
    protected void initListener() {
       team_list.setOnItemClickListener(this);

    }





    @Override
    public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
        //TODO 跳转到pictureActivity

        Intent intent = new Intent(this, PictureActivity.class);
        intent.putExtra(PictureActivity.KEY_INDEX,i);
        intent.putExtra(PictureActivity.KEY_PICTURES,(ArrayList<String>)imgs);
        startActivity(intent);
    }

    private void UpdateTeamInfo() {
        career_beging_tv.setText(teacher.getWorkTime()+"年");
        teacher_introduce_tv.setText(teacher.getIntroduce());
        performance_tv.setText(teacher.getAchievement());
        experince_tv.setText(teacher.getExperience());
        teacher_name_tv.setText(teacher.getTeacherName());
    }



    class ImageAdapter extends BaseAdapter{
        Context cnt;
        List<String> data;

        public ImageAdapter(Context cnt) {
            this.cnt = cnt;
        }

        public void setData(List<String> data){
            this.data=data;
            notifyDataSetChanged();
        }
        @Override
        public int getCount() {
            if (null==data||data.size()==0)
                return 0;
            else
                return data.size();
        }

        @Override
        public Object getItem(int position) {
            return data.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View v, ViewGroup parent) {
            ViewHolder holder;
            if (null==v){
                holder=new ViewHolder();
                v=View.inflate(cnt,R.layout.item_teacher_img,null);
                holder.ico=(ImageView)v.findViewById(R.id.img);
                v.setTag(holder);
            }else
                holder=(ViewHolder)v.getTag();
                String pic=data.get(position);
            //String url = String.format("%s%s?name=%s", Config.BASE_URL, Config.IMAGE_LOAD_URL, pic);
            ImageUtils.loadImage(cnt, holder.ico, pic, R.mipmap.def_user_ico);


            return v;
        }

        class ViewHolder {
            ImageView ico;

        }
    }
}
